﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.ServiceModel;
using ARR.DataModel.Repositories;

namespace ARR.Services.BizRules
{
    public static class CategoryMatchBizRules
    {
        public static void ValidateSave(CategoryMatch match)
        {
            if (!match.IsValid)
                throw new FaultException(match.Error);

            var previousMatches = CategoryMatchRepository.Select(x => x.CountryId == match.CountryId && x.Name == match.Name);
            bool IsInsert = match.GetEntityState() == DDF.Entity.EntityState.New;

            if (IsInsert && previousMatches.Count > 0)
                throw new FaultException<ARRExceptionDetail>(
                    new ARRExceptionDetail(string.Format("La categoria {0} ya tiene una equivalencia con una categoria NACS", match.Name), ExceptionSeverity.Validation));

        }
    }
}
